﻿/*
 * ********************************************************************* *
  
Copyright HLRS, University Stuttgart

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* ********************************************************************* *
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;

using ServiceLibrary;

namespace TestClient
{
    class Program
    {
        private static void CreateClient(int i, out ISampleService sampleService, out IClientChannel clientChannel)
        {
            string virtualService = string.Format("urn:serviceid:{0}", i.ToString());
            string gateway = "http://localhost:81/BREIN/GatewayService/innerEndpoint";

            WSHttpBinding binding = new WSHttpBinding(SecurityMode.None);

            SampleServiceClient client = new SampleServiceClient(binding, new EndpointAddress(gateway));
            
            sampleService = client.ChannelFactory.CreateChannel(
                new EndpointAddress(virtualService), new Uri(gateway));
            
            clientChannel = client.InnerChannel;
        }

        static void Main(string[] args)
        {
            System.Console.Title = "Testclient";

            int numberOfInvocations = Int32.Parse(args[0]);

            System.Console.Out.WriteLine("Press <Return> to invoke services");

            DateTime startTime; 
            DateTime endTime; 

            while (true)
            {
                System.Console.ReadLine();
                startTime = DateTime.Now;
                IClientChannel clientChannel = null;
                ISampleService client = null;
                //CreateClient(1, out client, out clientChannel);
                for (int i = 1; i <= numberOfInvocations; i++)
                {
                    CreateClient((i%10)+1, out client, out clientChannel);

                    using (OperationContextScope scope = new OperationContextScope(clientChannel))
                    {
                        System.Console.Out.WriteLine(client.GetData("ping"));
                    }
                }
                endTime = DateTime.Now;
                Console.WriteLine("Invoked test service " + numberOfInvocations + " times");
                Console.WriteLine("Started: " + startTime.ToString());
                Console.WriteLine("Finished: " + endTime.ToString());
                Console.WriteLine("Testrun duration: " + (endTime.Subtract(startTime)).TotalMilliseconds + "ms");
                Console.WriteLine("Average duration of each invocation: " + ((float)((endTime.Subtract(startTime)).TotalMilliseconds) / (float)numberOfInvocations) + "ms");
                Console.WriteLine("Invocations / second: " + (1000 / ((float)((endTime.Subtract(startTime)).TotalMilliseconds) / (float)numberOfInvocations))); 
            }           
        }
    }
}
